package com.tjcu.action;

import com.opensymphony.xwork2.ActionSupport;
import com.tjcu.entity.Clazz;
import com.tjcu.entity.Mark;
import com.tjcu.service.ClazzService;
import com.tjcu.service.impl.ClazzServiceImpl;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author 王恒杰
 * @version 1.0
 * @date 2021/11/2 14:08
 * @email 1078993387@qq.com
 * @Address 天津
 * @Description:
 */
public class ClazzAction extends ActionSupport {
    private Mark mark;
    private Clazz clazz;
    private    List<Clazz> clazzes;
    private List<Mark> marks;


    private File upload;
    /**
     * Struts2 框架提交数据获取文件上传文件原名的方法  方法名：uploadFileName
     */
    private String uploadFileName;
    /**
     * 当前文件的类型·
     */
    private String uploadContentType;
    /**
     * 添加班级信息
     */
    public String addClazz() throws IOException {
        //通过相对路径获取绝对路径
        String realPath = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/upload");
        System.out.println("真实路径"+realPath);
        System.out.println("上传文件的路径"+upload);

            FileUtils.copyFile(upload,new File(realPath+"/"+uploadFileName));

        //调用业务层
        ClazzService clazzService = new ClazzServiceImpl();
        //班级的实体类属性：clazzId,cname, cLogoPath,mark;
        clazz.setClazzId(null);
        System.out.println(clazz.getCname());
        System.out.println(mark.getMid());
        //班级logo的路径
        String path="/"+uploadFileName;
        clazz.setcLogoPath(path);
        System.out.println("path"+path);
        Clazz clazz1 = new Clazz(null,clazz.getCname(),path,mark);
        clazzService.addClazz(clazz1);

        return  SUCCESS;
    }
    /**
     * 删除班级信息
     */
    public String dropClazz(){
        //调用业务层
        ClazzService clazzService = new ClazzServiceImpl();
        Integer id = clazz.getClazzId();
        System.out.println(id);
        clazzService.dropClazz(id);
        return  SUCCESS;
    }
    /**
     * 查询班级信息
     */
    public String showClazz(){
        //调用业务层
        ClazzService clazzService = new ClazzServiceImpl();
        //  clazzes保含标签的名
          clazzes = clazzService.showClazz();
        return  SUCCESS;
    }
    /**
     * 查询标签信息
     */
    public String showMark(){
        //调用业务层
        ClazzService clazzService = new ClazzServiceImpl();
        marks = clazzService.queryMarkByType(mark.getMtype());
        return  SUCCESS;
    }

    public Mark getMark() {
        return mark;
    }

    public void setMark(Mark mark) {
        this.mark = mark;
    }

    public Clazz getClazz() {
        return clazz;
    }

    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }

    public List<Clazz> getClazzes() {
        return clazzes;
    }

    public void setClazzes(List<Clazz> clazzes) {
        this.clazzes = clazzes;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public List<Mark> getMarks() {
        return marks;
    }

    public void setMarks(List<Mark> marks) {
        this.marks = marks;
    }
}
